package regulation.auto.com.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;

import regulation.auto.com.AtuoRegulationApplication;
import regulation.auto.com.bean.LoginBean;

/**
 * Created by lidongming on 2018/3/7.
 * 用户登录信息
 */

public class UserManager {

    private Context mContext;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

    private static UserManager instance;


    public static UserManager getInstance() {
        if(instance == null){
            instance = new UserManager();
        }
        return instance;
    }


    private UserManager() {
        init(AtuoRegulationApplication.getInstance());
    }

    public void init(Context context) {
        if (context != null) {
            mContext = context.getApplicationContext();
        }
        int mode = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) ? Context.MODE_MULTI_PROCESS : Context.MODE_PRIVATE;
        mSharedPreferences = mContext.getSharedPreferences("user_info", mode);
        mEditor = mSharedPreferences.edit();
    }

    public void setUserName(String userName) {
        mEditor.putString("user_name", userName).commit();
    }

    public String getUserName() {
        return mSharedPreferences.getString("user_name", "");
    }


    public void setAccessToken(String accessToken) {
        mEditor.putString("access_token", accessToken).commit();
    }

    public String getAccessToken() {
        return mSharedPreferences.getString("access_token", "");
    }


    public void setLoginInfo(LoginBean loginBean) {
        if (!TextUtils.isEmpty(loginBean.getAccess_token())) {
            setAccessToken(loginBean.getAccess_token());
        }
    }
}
